package jmm.thread;

import java.util.HashMap;
import java.util.Map;

/**
 * ThreadLocal demo
 */
public class ThreadLocalTest {


    public static void main(String[] args) throws InterruptedException {

        ThreadLocalCache.THREAD_LOCAL.set("123123123");
        System.out.println(Thread.currentThread().getName() + ThreadLocalCache.THREAD_LOCAL.get());

        new Thread(()->{
            Map map = new HashMap();
            map.put("name","wen");
            ThreadLocalCache.THREAD_LOCAL.set(map);
            System.out.println(Thread.currentThread().getName() + ThreadLocalCache.THREAD_LOCAL.get());
        }).start();
        Thread.sleep(500);
        System.out.println(Thread.currentThread().getName() +"============="+ ThreadLocalCache.THREAD_LOCAL.get());
        System.out.println(value.get());
    }


    // 自己定义的 ThreadLocal，并且初始化赋值
    private static final ThreadLocal<Integer> value = ThreadLocal.withInitial(() -> Integer.valueOf(1));

}
